package com.zhuiYun.project.api.appletshoppingcart.mapper;

import com.zhuiYun.project.api.appletshoppingcart.entity.AppletShoppingCartEntity;
import com.zhuiYun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author lyp
 * @className AppletShoppingCartMapper
 * @description 小程序购物车mapper层
 * @date 2023/4/5 9:39
 */
@Mapper
public interface AppletShoppingCartMapper extends BaseMapper<AppletShoppingCartEntity> {

    /**
     * @author lyp
     * @description 查询购物车所有商品的记录
     * @date 2023/4/5 10:27
     * @return List<AppletShoppingCartEntity>
     */
    @Select("<script>SELECT a.*,b.commodity_name as comm_name,b.image_id as img_id,b.favorable_price as fav_price,b.price as price,b.del_flag as d_flag,b.back as back FROM shopping_cart a LEFT JOIN commodity b on a.commodity_id = b.id WHERE user_id = #{userId} AND a.del_flag = 0 AND a.is_no_diy = 0</script>")
    List<AppletShoppingCartEntity> getAllInfo(Integer userId);

    /**
     * @author lyp
     * @description 查询购物车所有diy的记录
     * @date 2023/4/5 10:27
     * @return List<AppletShoppingCartEntity>
     */
    @Select("<script>SELECT a.*,b.diy_name,b.diy_price,b.diy_favorable_price,b.diy_image_id,b.del_flag as de_flag FROM shopping_cart a LEFT JOIN handwork_diy b on a.diy_id = b.id WHERE a.user_id = #{userId} AND a.del_flag = 0 AND a.is_no_diy = 1</script>")
    List<AppletShoppingCartEntity> getDiyInfo(Integer userId);

    /**
     * @author lyp
     * @description 查询购物车一天内或者十天内所有diy的记录
     * @date 2023/4/5 10:27
     * @return List<AppletShoppingCartEntity>
     */
    @Select("<script>SELECT a.*,b.diy_name,b.diy_price,b.diy_favorable_price,b.diy_image_id,b.del_flag as de_flag FROM shopping_cart a LEFT JOIN handwork_diy b on a.diy_id = b.id WHERE add_to_cart_date >= DATE_SUB(NOW(), INTERVAL #{day} DAY) AND user_id = #{userId} AND a.del_flag = 0 AND a.is_no_diy = 1</script>")
    List<AppletShoppingCartEntity> getInOneOrTenDayDiyInfo(Integer day,Integer userId);

    /**
     * @author lyp
     * @description 查询购物车十天内所有diy的记录
     * @date 2023/4/5 10:27
     * @return List<AppletShoppingCartEntity>
     */
    @Select("<script>SELECT a.*,b.diy_name,b.diy_price,b.diy_favorable_price,b.diy_image_id,b.del_flag as de_flag FROM shopping_cart a LEFT JOIN handwork_diy b on a.diy_id = b.id WHERE a.user_id = #{userId} AND a.del_flag = 0 AND a.is_no_diy = 1</script>")
    List<AppletShoppingCartEntity> getInOneMonthDiyInfo(Integer month,Integer userId);

    /**
     * @author lyp
     * @description 查询购物车一,十天内的记录
     * @date 2023/4/5 10:27
     * @param day
     * @return List<AppletShoppingCartEntity>
     */
    @Select("<script>SELECT a.*,b.commodity_name as comm_name,b.image_id as img_id,b.favorable_price as fav_price,b.price as price,b.del_flag as d_flag,b.back as back FROM shopping_cart a LEFT JOIN commodity b on a.commodity_id = b.id WHERE add_to_cart_date >= DATE_SUB(NOW(), INTERVAL #{day} DAY) AND user_id = #{userId} AND a.del_flag = 0 AND a.is_no_diy = 0</script>")
    List<AppletShoppingCartEntity> getInOneOrTenDayInfo(Integer day,Integer userId);

    /**
     * @author lyp
     * @description 查询购物车一月内的记录
     * @date 2023/4/5 10:27
     * @param month
     * @return List<AppletShoppingCartEntity>
     */
    @Select("<script>SELECT a.*,b.commodity_name as comm_name,b.image_id as img_id,b.del_flag as d_flag,b.favorable_price as fav_price,b.price as price,b.back as back FROM shopping_cart a LEFT JOIN commodity b on a.commodity_id = b.id WHERE add_to_cart_date >= DATE_SUB(NOW(), INTERVAL #{month} MONTH) AND user_id = #{userId} AND a.del_flag = 0 AND a.is_no_diy = 0</script>")
    List<AppletShoppingCartEntity> getInOneMonthInfo(Integer month,Integer userId);

    /**
     * @author lyp
     * @description 删除购物车内的商品
     * @date 2023/4/6 20:08
     * @param: id 物品id
     * @param: userId 用户id
     * @return CommonResult
     */
    @Update("<script>UPDATE shopping_cart SET del_flag = 1 WHERE id = #{id} AND user_id = #{userId}</script>")
    int delItemsInShoppingCart(Integer id,Integer userId);

    /**
     * @author lyp
     * @description 持久化用户购物车信息 例如 ：加了一个商品数量等
     * @date 2023/4/7 11:46
     * @param userId
     * @param id
     * @param number
     * @return CommonResult
     */
    @Update("<script>UPDATE shopping_cart SET number = #{number} WHERE id = #{id} AND user_id = #{userId}</script>")
    int persistingUserShoppingCartInfo(Integer userId,Integer id, Integer number);

    /**
     * @Author lzk
     * @Description 计算购物车数量
     * @date 2023/4/7 16:37
     * Return java.util.List<com.zhuiYun.project.api.appletshoppingcart.entity.AppletShoppingCartEntity>
     */
    @Select("select count(*) as total from shopping_cart where user_id = #{userId} ")
    Integer calculateTheNumberOfShoppingCarts(@Param("userId") Integer userId);

    /**
     * @author lyp
     * @description 持久化用户选中商品 （全部物品）
     * @date 2023/4/8 9:22
     * @param: flag
     * @param: userId
     * @return CommonResult
     */
    @Update("<script>UPDATE shopping_cart SET flag = #{flag} WHERE user_id = #{userId} AND del_flag = 0</script>")
    int persistingFlag(String flag,Integer userId);

    /**
     * @author lyp
     * @description 持久化用户选中单个商品（单个物品）
     * @date 2023/4/8 9:22
     * @param: flag
     * @param: userId
     * @return CommonResult
     */
    @Update("<script>UPDATE shopping_cart SET flag = #{flag} WHERE user_id = #{userId} AND del_flag = 0 AND id = #{id}</script>")
    int persistingOneFlag(String flag,Integer userId,Integer id);

    /**
     * @author lyp
     * @description 用户结算购物车内商品，进行查询购物车内商品信息
     * @date 2023/4/11 14:46
     * @param: userId
     * @return List<AppletShoppingCartEntity>
     */
    @Select("<script>SELECT a.*,b.commodity_name as comm_name,b.image_id as img_id,b.favorable_price as fav_price,b.price as price,b.del_flag as d_flag,b.back as back FROM shopping_cart a LEFT JOIN commodity b on a.commodity_id = b.id WHERE user_id = #{userId} AND a.del_flag = 0 AND a.commodity_id>0 AND a.flag='true'</script>")
    List<AppletShoppingCartEntity> getShoppingCartInfo(Integer userId);

    /**
     * @author lyp
     * @description 用户结算购物车内diy，进行查询购物车内diy信息
     * @date 2023/4/11 14:46
     * @param: userId
     * @return List<AppletShoppingCartEntity>
     */
    @Select("<script>SELECT a.*,b.diy_name,b.diy_price,b.diy_favorable_price,b.diy_image_id,b.del_flag as de_flag FROM shopping_cart a LEFT JOIN handwork_diy b on a.diy_id = b.id WHERE a.user_id = #{userId} AND a.del_flag = 0 AND a.is_no_diy = 1 AND a.flag='true'</script>")
    List<AppletShoppingCartEntity> getShoppingCartInfoDiy(Integer userId);
}
